package com.ruleengine.container;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ruleengine.compiler.ITaskCompiler;
import com.ruleengine.instruction.InstructionSet;
import com.ruleengine.task.config.TaskConfigManager;
import com.ruleengine.task.impl.WorkflowTask;

public class WorkflowContainer {
    private static Logger logger = Logger.getLogger(WorkflowContainer.class);
    private Map<String, WorkflowTask> tasks;
    private Map<String, InstructionSet> instructions;
    private volatile static WorkflowContainer container;

    private WorkflowContainer() {
        tasks = new HashMap<String, WorkflowTask>();
        instructions = new HashMap<String, InstructionSet>();
    }

    public static  WorkflowContainer getInstance() {
        if (container == null) {
            synchronized(WorkflowContainer.class){
                if (container == null) {
                    container = new WorkflowContainer();
                }
            }
        }
        return container;
    }

    /**
     * 注册（新增）或更新工作流配置
     * 
     * @param task
     *            工作流配置
     */
    public synchronized void registerWorkflowTask(WorkflowTask task) {
        if (tasks.get(task.getName()) == null) {
            logger.debug("the workflow task had not been registered, add it :"
                    + task.getName());
        }
        tasks.remove(task.getName());
        tasks.put(task.getName(), task);
        logger.debug("the workflow task has been registered successfully : "
                + task.getName());
    }

    /**
     * 删除工作流配置
     * 
     * @param taskId
     *            工作流配置id
     * @return 删除的工作流配置
     */
    public synchronized WorkflowTask removeWorkflowTask(String taskId) {
        WorkflowTask task = tasks.remove(taskId);
        if (task == null) {
            logger.debug("the workflow task  been deleteing is not exsit : "
                    + taskId);
        }
        return task;
    }

    /**
     * 获取工作流配置
     * 
     * @param taskId
     *            工作流配置id
     * @return 工作流配置
     */
    public WorkflowTask getWorkflowTask(String taskId) {
        WorkflowTask task = tasks.get(taskId);
        if (task == null) {
            logger.debug("the workflow task  been finding is not exsit : "
                    + taskId);
        }
        return task;
    }

    /**
     * 获取工作流指令
     * 
     * @param taskId
     *            工作流配置id
     * @return 工作流指令
     */
    public InstructionSet getWorkflowInstruction(String taskId) {
        if (instructions.get(taskId) == null) {
            // 指令还没有编译
            
            WorkflowTask task = tasks.get(taskId);
            if (task == null) {
                // 渠道工作流配置没有存在
                throw new RuntimeException("the workflow define file  been finding is not exsit : "
                        + task);
            }

            logger.debug("the " + taskId + " task :" + task);
            
            synchronized (instructions) {
                if (instructions.get(taskId) == null) {
                    // 编译指令
                    try {
                        // 获取指令编译器
                        ITaskCompiler c = TaskConfigManager.getInstance()
                                .getTaskCompiler(task.getTaskType());
                        InstructionSet n = new InstructionSet();
                        n.setInstructionSetId(taskId);
                        c.comipleTask(task,n);
                        logger
                                .debug("the workflow Instruction  been compiled sucessfully : "
                                        + n);
                        instructions.put(taskId, n);
                    } catch (Exception e) {
                        logger.error(e, e);
                    }
                }
            }
        }
        return instructions.get(taskId);
    }
    
    

}
